package cn.edu.xjtu.android.emoji_warfare_android.game.hero;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import cn.edu.xjtu.android.emoji_warfare_android.engine.Debug;
import cn.edu.xjtu.android.emoji_warfare_android.engine.entity.Point;
import cn.edu.xjtu.android.emoji_warfare_android.engine.shape.Shape;
import cn.edu.xjtu.android.emoji_warfare_android.game.Bullet;
import cn.edu.xjtu.android.emoji_warfare_android.game.Config;
import cn.edu.xjtu.android.emoji_warfare_android.game.Engine;

/**
 * Boss
 * 
 * @author bill wyyf
 *
 */
public class Boss extends Enemy {
    private static int index = 0;

    public int speedX = 1;

    public int status = 0;

    static Bitmap pic = Config.Hero.bosses[index % 6];

    private boolean debug = false;

    /**
     * 构造一个Boss
     * 
     * @param level
     *            Boss等级
     * @param outline
     *            碰撞箱形状
     */
    public Boss(int level, Shape outline) {
        super(outline);
        hp = (level + 20) * 60;
        Boss.pic = Config.Hero.bosses[index % 6];
        Boss.index++;
    }

    @Override
    public boolean onDraw(Canvas canvas) {
        canvas.drawBitmap(pic, -225, -150, Config.Common.bitmapPaint);
        if (debug) {
            Debug.drawCross(canvas, 0, 0);
            Debug.printText(canvas, String.format("(%f, %f)", basePoint.x, basePoint.y));
        }
        if (inDamage) {
            canvas.drawRect(-225, -150, 225, 150, paint);
            inDamage = false;
        }
        return false;
    }

    /**
     * 发射单颗子弹
     * 
     * @param speed
     *            速度
     */
    public void fire(int speed) {
        float y = this.basePoint.y + 150;
        float x = this.basePoint.x;
        Point p = Engine.instance.getDefaultController().player.getBasePoint();
        float x2 = p.x;
        float y2 = p.y;
        float dx = x2 - x;
        float dy = y2 - y;
        float r = (float) (Math.sqrt(dx * dx + dy * dy));
        dx = (dx / r) * speed;
        dy = (dy / r) * speed;
        Bullet b = new Bullet(this.basePoint.x, this.basePoint.y, dx, dy, level, 1);
        b.atk = 5;
        Engine.instance.addEnemyBullet(b);
    }

    /**
     * 发射多颗子弹
     * 
     * @param speed
     *            速度
     * @param xs
     *            水平位置数组
     */
    public void fire(int speed, float[] xs) {
        for (int i = 0; i < xs.length; i++) {
            float y = this.basePoint.y + 150;
            float x = this.basePoint.x + xs[i];
            Point p = Engine.instance.getDefaultController().player.getBasePoint();
            float x2 = p.x;
            float y2 = p.y;
            float dx = x2 - x;
            float dy = y2 - y;
            float r = (float) (Math.sqrt(dx * dx + dy * dy));
            dx = (dx / r) * speed;
            dy = (dy / r) * speed;
            Bullet b = new Bullet(this.basePoint.x + xs[i], this.basePoint.y, dx, dy, level, 1);
            b.atk = 5;
            Engine.instance.addEnemyBullet(b);
        }
    }
}
